<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" xmlns:dic="http://www.w3.org/1999/xhtml">
<body class="list">
<div th:replace="include/head.html"></div>
<div class="layui-fluid  layui-anim-upbit layui-anim">
    <div class="layui-card">
        <!--查询from 表单-->
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">登录账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="account" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <!--<div class="layui-inline">
                    <label class="layui-form-label">日期范围</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" readonly id="laydate" placeholder=" - ">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">日期范围</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" readonly id="startTime" placeholder=" - ">
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" readonly id="endTime" placeholder=" - ">
                    </div>
                </div>-->
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <!--表格中的数据 并不显示-->
        <div class="layui-card-body">
            <!--表格外部上侧的按钮-->
            <div style="padding-bottom: 10px;" class="tool-bar">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add" shiro:hasPermission="sysuser:edit">添加</button>
            </div>
            <!--数据表格 -->
            <table id="table" lay-filter="table"></table>
            <!--表格每行的 的操作按钮 -->
            <script type="text/html" id="toolbar">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"
                   shiro:hasPermission="sysuser:edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                {{#  if(d.oid != '1' ){ }}
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"
                   shiro:hasPermission="sysuser:delBean"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
                {{#  } }}
            </script>
        </div>
    </div>
</div>
<script type="text/javascript" th:inline="none">
    var table ;
    layui.use(['jquery','table', 'laydate'], function () {
        var $ = layui.$;
        table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        //日期范围
        laydate.render({
            elem: '#laydate'
            , range: true
            , theme: '#393D49'
        });
        //分两个的时间空间
        var start = laydate.render({
            elem: '#startTime',
            //value: new Date(),
            done: function (value, date) {
                end.config.min = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                    hours: date.hours,
                    minutes: date.minutes,
                    seconds: date.seconds
                }
            }
        });
        var end = laydate.render({
            elem: '#endTime',
            //value: new Date(),
            done: function (value, date) {
                start.config.max = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                    hours: date.hours,
                    minutes: date.minutes,
                    seconds: date.seconds
                }
            }
        });
        table.render({
            elem: '#table'
            , id: 'table'
            , loading: true
            , method: 'post'
            , url: 'sysuser/listPage' //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {type: 'numbers', fixed: 'left', title: '序号'}
                , {field: 'account', title: '登录账号'}
                , {field: 'name', title: '用户名'}
                , {field: 'userType', title: '角色'}
                , {fixed: 'right', width: 165, align: 'center', toolbar: '#toolbar'}
            ]]
        });
        //监听搜索
        form.on('submit(search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('table', {where: field});
        });
        //监听行工具事件
        //注：tool 是工具条事件名，table 是 table 原始容器的属性 lay-filter="对应的值"
        table.on('tool(table)', function (obj) {
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'detail') {
                layer.msg('查看操作');
            } else if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.post("sysuser/delBean",{oid:data.oid},function (res) {
                        //向服务端发送删除指令
                        if(res.code === 0){
                            layer.msg(res.msg);
                            obj.del(); //删除对应行（tr）的DOM结构
                            layer.close(index);
                            //table.reload('table');
                        }else {
                            layer.msg(res.msg);
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                editLayer({
                    title:'编辑用户',
                    url:'sysuser/edit?oid='+data.oid,
                    area:['800px','500px'],
                    back:callback
                });
            }
        });
        $('.tool-bar .layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        //事件
        var active = {
            add: function () {
                var adduser =  editLayer({
                    title:'添加用户',
                    url:'sysuser/edit',
                    area:['800px','500px'],
                    back: callback
                });
                //layer.full(adduser);
            }
        };
    });
    function callback(ifr,index,data){
        var field = data.field;
        $.post("sysUserRole/saveOrUpdate",field,function (res) {
            if(res.code === 0){
                topLayer.msg(res.msg);
                topLayer.close(index);
                table.reload('table');
            }else {
                topLayer.msg(res.msg);
            }
        });
        topLayer.close(index);
    }
</script>
</body>
</html>
